package lv.imho.server.services.population;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lv.imho.client.model.DataSourceTransferObject;
import lv.imho.client.services.DataService;
import lv.imho.server.services.DataServiceImpl;

/**
 * Populates table with data sources. This is more for debugging purposes, however easily can be used
 * in production mode.
 * 
 * @author Igors Gulbinskis
 */
public class DataSourcePopulationService extends HttpServlet {

	private static final Logger logger = Logger.getLogger(DataSourcePopulationService.class.getName());
	private static final long   serialVersionUID = 1L;
	
	private DataService dataService;
	
	public DataSourcePopulationService() {
		dataService = new DataServiceImpl();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 	
		throws ServletException, IOException {
		logger.info("request for datasource population");
		
		int total = 0;
		
		try {
			/* news */			
			DataSourceTransferObject dataSource = new DataSourceTransferObject();
			dataSource.setEnabled(true);
			dataSource.setName("Ziņas: TVNET");
			dataSource.setUrl("http://www.tvnet.lv");
			dataSource.setFeedUrl("http://feeds.tvnet.lv/tvnet/all/latest?format=xml");
			if (!dataService.exists(dataSource)) {
				dataService.saveDataSource(dataSource);
				total++;
			}
				
			/* blogs */
			dataSource = new DataSourceTransferObject();
			dataSource.setEnabled(true);
			dataSource.setName("Blogs: Laacz");
			dataSource.setUrl("http://www.laacz.lv");
			dataSource.setFeedUrl("http://feeds.feedburner.com/laacz");
			if (!dataService.exists(dataSource)) {
				dataService.saveDataSource(dataSource);
				total++;
			}
			
			/* blogs */
			dataSource = new DataSourceTransferObject();
			dataSource.setEnabled(true);
			dataSource.setName("Blogs: Kārlis Streips");
			dataSource.setUrl("http://karlisstreips.blogspot.com");
			dataSource.setFeedUrl("http://karlisstreips.blogspot.com/feeds/posts/default?alt=rss");
			if (!dataService.exists(dataSource)) {
				dataService.saveDataSource(dataSource);
				total++;
			}
			
			/* tweets */
			dataSource = new DataSourceTransferObject();
			dataSource.setEnabled(true);
			dataSource.setName("Twitter: Arturs Mednis");
			dataSource.setUrl("http://twitter.com/arturs");
			dataSource.setFeedUrl("http://twitter.com/statuses/user_timeline/975701.rss");
			if (!dataService.exists(dataSource)) {
				dataService.saveDataSource(dataSource);
				total++;
			}
			
			dataSource = new DataSourceTransferObject();
			dataSource.setEnabled(true);
			dataSource.setName("Twitter: Krizdabs");
			dataSource.setUrl("http://twitter.com/krizdabz");
			dataSource.setFeedUrl("http://twitter.com/statuses/user_timeline/5607902.rss");
			if (!dataService.exists(dataSource)) {
				dataService.saveDataSource(dataSource);
				total++;
			}
						
			logger.info("data sources successfully populated");
			
			resp.getWriter().println(
				"<html>" +
					"<title>inmyhumbleopinion.lv | Data source population</title>" +
					"<body>" +
						"<h2>Data sources successfully populated!</h2><p>" + total + " new data sources</p>" +
					"</body>" +
				"</html>"				
			);
		} catch (Exception e) {
			resp.getWriter().println(
				"<html>" +
					"<title>inmyhumbleopinion.lv | Data source population</title>" +
					"<body>" +
						"<h2>Some errors occured during the data population (refer to logs)</h2>" +
					"</body>" +
				"</html>"				
			);
		}
	}
	
}
